/**
 * 
 */
package prv.pursuivant.render.html;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import prv.pursuivant.core.AchievType;
import prv.pursuivant.core.AchievTypesTree;
import prv.pursuivant.core.impl.KindImpl;
import prv.pursuivant.render.Destination;
import prv.pursuivant.render.OutputGenerator;
import prv.pursuivant.render.PageDecorationRenderer;
import prv.pursuivant.util.DateUtil;

import com.google.inject.Inject;
import com.google.inject.internal.Lists;

/**
 * @author Kwinto
 * 
 */
public class HtmlPageDecorationRenderer implements PageDecorationRenderer {

	private final AchievTypesTree tree;
	private final Collection<String> activeAchievs;
	private final Destination destination;
	private final DateUtil dateUtil;

	@Inject
	public HtmlPageDecorationRenderer(AchievTypesTree tree,
			Collection<String> activeAchievs, Destination destination,
			DateUtil dateUtil) {
		this.tree = tree;
		this.activeAchievs = activeAchievs;
		this.destination = destination;
		this.dateUtil = dateUtil;
	}

	public void renderPageHeader(OutputGenerator generator, AchievType achiev,
			Date when) {
		generator.elem("html");
		generator.elem("head");
		generator.emptyElem("meta").attr("http-equiv", "Content-type").attr(
				"content", "text/html;charset=UTF-8");
		generator.emptyElem("link").attr("rel", "stylesheet").attr("type",
				"text/css").attr("href", "../style/pursuivant.css");
		generator.elem("script").attr("type", "text/javascript").attr("src",
				"http://static.wowhead.com/widgets/power.js").end();
		generator.elem("title").text(
				achiev.getName() + " - " + dateUtil.format(when)).end();
		generator.end();
		generator.elem("body").elem("div").attr("id", "main");
		generator.elem("div").attr("class", "achievList").attr("id", "top");
		generator.emptyElem("img").attr("src", "../style/guild-logo.png").attr(
				"alt", "").attr("width", "643").attr("height", "118");
		// renderAchievList(generator, achiev);
		generator.end();

		generator.elem("div").attr("id", "content");
	}

	public void renderPageFooter(OutputGenerator generator, AchievType achiev,
			Date when) {
		generator.end(); // div#content
		generator.elem("div").attr("class", "achievList").attr("id", "bottom");
		renderAchievList(generator, achiev);
		generator.end();

		generator.elem("div").attr("id", "about");
		generator.elem("span").text("Generated by ").elem("a").attr("href",
				"http://code.google.com/p/pursuivant/").text("Pursuivant")
				.end().end();
		generator.end();

		generator.end(); // div#main
		generator.end(); // body
		generator.end(); // html
	}

	public void renderAchievList(OutputGenerator generator,
			AchievType selectedAchiev) {

		List<AchievType> categories = Lists.newArrayList();
		List<AchievType> metas = Lists.newArrayList();
		List<AchievType> other = Lists.newArrayList();

		for (String id : activeAchievs) {
			AchievType achiev = tree.findAchievTypeById(id);
			if (achiev.getKind() == KindImpl.CATEGORY
					|| achiev.getKind() == KindImpl.SUBCATEGORY) {
				categories.add(achiev);
			} else if (achiev.getKind() == KindImpl.META) {
				metas.add(achiev);
			} else
				other.add(achiev);
		}

		renderList(generator, "Kategorie", categories, selectedAchiev);
		renderList(generator, "Meta", metas, selectedAchiev);
		renderList(generator, "Inne", other, selectedAchiev);

	}

	/**
	 * @param generator
	 * @param name
	 * @param achievs
	 * @param selectedAchiev
	 */
	private void renderList(OutputGenerator generator, String name,
			List<AchievType> achievs, AchievType selectedAchiev) {
		if (achievs.isEmpty())
			return;
		generator.text(name + ":");
		for (AchievType achiev : achievs) {
			generator.elem("span").text(" ").entRef("bull").text(" ");// .entRef("nbsp");
			addAchievText(generator, achiev, !achiev.getId().equals(
					selectedAchiev.getId()));
			generator.end();
			// generator.text(" ");
		}
		generator.emptyElem("br");
		generator.text(""); // this is to make sure that <br /> will be closed
	}

	/**
	 * @param generator
	 * @param current
	 */
	private void addAchievText(OutputGenerator generator, AchievType current,
			boolean makeLink) {
		if (makeLink)
			generator.elem("a").attr("href", getPageLink(current));
		StringTokenizer st = new StringTokenizer(current.getName(), " ", false);
		if (st.hasMoreTokens())
			generator.text(st.nextToken());
		while (st.hasMoreTokens()) {
			generator.entRef("nbsp");
			// generator.text(" ");
			generator.text(st.nextToken());
		}
		if (makeLink)
			generator.end();
		generator.entRef("nbsp");
		generator.text("(" + current.getCriteria().size() + ")");
	}

	/**
	 * @param selectedAchiev
	 * @return
	 */
	private String getPageLink(AchievType selectedAchiev) {
		return destination.getUriForPage(selectedAchiev.getId());
	}
}
